<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="s" uri="http://www.springframework.org/tags/form" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>采购批量入库</title>
    <jsp:include page="easyuiIncludeJS.jsp"/>

    <script type="text/javascript">
        $(function () {
            var pager = $(".easyui-datagrid").datagrid('getPager');
            $(pager).pagination({
                total:${pager.totalRows},
                pageSize:${pager.pageRow},
                displayMsg: '{from}-{to}/{total}',
                pageNumber:${pager.cur_page},
                afterPageText: '页，共{pages}页',
                pageList: [5, 10, 20, 30, 50, 100],
                loading: false,
                showPageList: true,
                showRefresh: true,
                //刷新方法
                onBeforeRefresh: function (p, s) {
                    location.href = "${pageContext.request.contextPath }/admin/materialInput/list?cur_page=" + p + "&pageRow=" + s;
                },
                onSelectPage: function (p, s) {
                    location.href = "${pageContext.request.contextPath }/admin/materialInput/list?cur_page=" + p + "&pageRow=" + s;
                }
            });
        });

        function ck(id) {
            window.location.href = "${pageContext.request.contextPath}/admin/materialInput/ck?worklogId="+id+"&ck=1";
        }

        function ss(val) {
        	$("#oprFlag").val(val);
            document.forms[0].submit();
        }
		function updateAduit(status){
			var ids = $('input[name="id"]:checked');
			var len = ids.length;
			if(ids.length==0){
				alert("请选择要审核的数据。");
				return;
			}
			if(!confirm('审核后的数据不能修改，请确认')){
				return;
			}			
			var arr="";
			ids.each(function(i,d){
				if((i+1)!=len){
					arr += $(d).val()+",";
				}else{
					arr += $(d).val();
				}
			})
			var url = "${pageContext.request.contextPath }/admin/materialInput/updateAduit?status=2&ids="+arr;
			location.href=url;	
		}
        function upload() {
            window.location.href="${pageContext.request.contextPath}/admin/materialInput/miUpload";
        }
      	
		function del(){
			var ids = $('input[name="id"]:checked');
			var len = ids.length;
			if(ids.length==0){
				alert("请选择要删除的数据,只有未审核的数据才能被删除。");
				return;
			}
			if(!confirm('只有未审核的数据才能被删除,请确认')){
				return;
			}			
			var arr="";
			ids.each(function(i,d){
				if((i+1)!=len){
					arr += $(d).val()+",";
				}else{
					arr += $(d).val();
				}
			})
			var url = "${pageContext.request.contextPath }/admin/materialInput/del?ids="+arr;
			location.href=url;	
		}
		function storage(){
			var rows = $("#dgitem").datagrid("getSelections"); 
			var i;
			if(rows.length<=0){
				alert("请选择要入库的订单。");
				return;
			}
			var arr="";
			var len = rows.length;
			var supplierName="";
			for(i=0;i<len;i++){
				var row = rows[i];
				if(row.aduitStatus=='未审核'){
					alert("包含有未审核的订单，请审核后再做入库操作.");
					return;
				}
				if(row.aduitStatus=='已入库'){
					alert("包含有已入库的订单，不允许重复入库.");
					return;
				}
				
				if(supplierName !='' && supplierName != row.supplierName){
					alert("一笔入库单只能选择同一个供应商的订单");
					return;
				}
				supplierName = row.supplierName;
				
				if((i+1)!=len){
					arr += row.id+",";
				}else{
					arr += row.id;
				}
			}
			if(!confirm('入库确认')){
				return;
			}	
			var url = "${pageContext.request.contextPath }/admin/materialInput/inStorage?ids="+arr;
			location.href=url;	
		}
    </script>
</head>
<body>
<div id="tb">
	<table width="100%" border="0">
		<tr>
			<td>
				<a class="easyui-linkbutton" plain="true" icon="icon-add" href="javascript:void(0);" onclick="upload();">导入</a>
			    <a class="easyui-linkbutton" plain="true" icon="icon-undo" href="javascript:void(0);" onclick="ss(1);">导出</a>
			    <a class="easyui-linkbutton" plain="true" icon="icon-edit" href="javascript:void(0);" onclick="updateAduit(2);">审核</a>
			    <a class="easyui-linkbutton" plain="true" icon="icon-edit" href="javascript:void(0);" onclick="storage();">入库</a>
			    <a class="easyui-linkbutton" plain="true" icon="icon-cut" href="javascript:void(0);" onclick="del();">删除</a>
			</td>
			<td align="right">
			    <form id="form1" modelAttribute="enrollment" action="${pageContext.request.contextPath}/admin/materialInput/list" method="post">
				   <input type="hidden" name="oprFlag" id="oprFlag" value="0">    
                    供应商: <select  id="supplierId" name="supplierId">
                        <option value="0">不选择</option>
                        <c:forEach items="${supplierList }" var="d">
                            <c:if test="${materialInput.supplierId==d.supplierId}">
                                <option value="${d.supplierId}" selected="selected">${d.supplierName}(${d.supplierCode})</option>
                            </c:if>
                            <c:if test="${materialInput.supplierId!=d.supplierId}">
                                <option value="${d.supplierId}">${d.supplierName}(${d.supplierCode})</option>
                            </c:if>
                        </c:forEach>
                    </select>
                    物料名称: <input type="text" id="materialName" name="materialName" value="${materialInput.materialName}" style="width:110px;">
                    批号: <input type="text" id="batchNo" name="batchNo" value="${materialInput.batchNo}" style="width:110px;">
                    状态：
                   <select name="aduitStatus" id="aduitStatus">
                    <option value="0" <c:if test="${materialInput.aduitStatus==0}">selected</c:if>>不选择</option>
                    <option value="1" <c:if test="${materialInput.aduitStatus==1}">selected</c:if>>未审核</option>
                    <option value="2" <c:if test="${materialInput.aduitStatus==2}">selected</c:if>>已审核</option>
                    <option value="3" <c:if test="${materialInput.aduitStatus==3}">selected</c:if>>已入库</option>
                   </select>
                    日期:<input class="easyui-datebox" id="oprDate" name="oprDate" value="${materialInput.oprDate}" editable="false"  style="width:130px;">
				   -<input class="easyui-datebox" id="oprDate2" name="oprDate2" value="${materialInput.oprDate2}" editable="false" style="width:130px;">
			        <a href="javascript:void(0);" class="easyui-linkbutton" plain="true" onclick="ss(0);" iconCls="icon-search" >搜索</a>
			    </form>
			</td>
		</tr>
	</table>
</div>
<table class="easyui-datagrid" id="dgitem" pagination="true" style="height:550px;width: auto;" singleSelect="false">
    <thead>
    <tr>
        <th width="50" align="center" checkbox="true" field="id" >选择</th>
        <th width="150" align="center" field="supplierName">供应商</th>
        <th width="120" align="center" field="oprDate">到货日期</th>        
        <th width="100" align="center" field="storageName">入库仓库</th>
        <th width="60" align="center" field="materialCode">物料编码</th>
        <th width="100" align="center" field="materialName">物料名称</th>
        <th width="60" align="center" field="style">规格型号</th>
        <th width="60" align="center" field="unitName">计量单位</th>
        <th width="90" align="center" field="orderId">采购单号</th>
        <th width="130" align="center" field="batchNo">生产批号</th>
        <th width="60" align="center" field="orderCnt">数量</th>
        <th width="80" align="center" field="aduitStatus">审核状态</th>
        <th width="150" align="center" field="orderNo">入库单号</th>
        <th width="80" align="center" field="userName">操作员</th>
    </tr>
    </thead>
    <tbody>
    <c:forEach items="${pager.datas}" var="d">
        <tr>
            <td>${d.materialInputId}</td>
            <td>${d.supplierName}</td>
            <td>${d.oprDate.substring(0,10)}</td>
            <td>${d.storageName}</td>
            <td>${d.materialCode}</td>
            <td>${d.materialName}</td>
            <td>${d.style}</td>
            <td>${d.unitName}</td>
            <td>${d.orderId}</td>
            <td>${d.batchNo}</td>
            <td>${d.orderCnt}</td>
            <td><c:if test="${d.aduitStatus==1}">未审核</c:if><c:if test="${d.aduitStatus==2}">已审核</c:if><c:if test="${d.aduitStatus==3}">已入库</c:if></td>
			<td>${d.orderNo}</td>
            <td>${d.userName}</td>
        </tr>
    </c:forEach>
    </tbody>
</table>
</body>
</html>